package vn.com.shoppie.object;

import java.util.ArrayList;

import vn.com.shoppie.activity.R;
import vn.com.shoppie.sobject.Campaign;
import vn.com.shoppie.sobject.Gift;
import vn.com.shoppie.sobject.Product;
import vn.com.shoppie.sobject.ShoppieObject;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BrandAdapter extends ArrayAdapter<ShoppieObject> {
	private ArrayList<ShoppieObject> mListShops;
	private LayoutInflater mInflater;

	public BrandAdapter(Context context, ArrayList<ShoppieObject> objects) {
		super(context, R.layout.item_title_shop, objects);
		this.mListShops = objects;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		if (mListShops.size() % 2 == 0)
			return mListShops.size() / 2;
		else
			return mListShops.size() / 2 + 1;
	}

	@Override
	public ShoppieObject getItem(int position) {
		System.out.println("get Item: " + position);
		return mListShops.get(position);
	}

	@Override
	public long getItemId(int position) {
		System.out.println("get Id: " + position);
		return (long) position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			ShopHolder _holder = new ShopHolder();
			v = this.mInflater.inflate(R.layout.item_title_shop, parent, false);
			_holder.shop1 = (RelativeLayout) v.findViewById(R.id.ly_shop1);
			_holder.shop2 = (RelativeLayout) v.findViewById(R.id.ly_shop2);
			_holder.avatar1 = (ImageView) v.findViewById(R.id.iv_avatar_1);
			_holder.avatar2 = (ImageView) v.findViewById(R.id.iv_avatar_2);
			_holder.title1 = (TextView) v.findViewById(R.id.tv_title_1);
			_holder.title2 = (TextView) v.findViewById(R.id.tv_title_2);
			_holder.score1 = (TextView) v.findViewById(R.id.tv_score_1);
			_holder.score2 = (TextView) v.findViewById(R.id.tv_score_2);
			_holder.pie1 = (ImageButton) v.findViewById(R.id.btn_score_1);
			_holder.pie2 = (ImageButton) v.findViewById(R.id.btn_score_2);

			_holder.avatar1.setImageResource(R.drawable.images);
			_holder.avatar2.setImageResource(R.drawable.images);
			v.setTag(_holder);
		}
		ShopHolder _holder = (ShopHolder) v.getTag();

		ShoppieObject item1 = mListShops.get(2 * position);
		ShoppieObject item2 = null;

		if (mListShops.size() / 2 > position) {
			item2 = mListShops.get(2 * position + 1);
		}
		fillShopToView(_holder, item1, item2);
		return v;
	}

	private void fillShopToView(final ShopHolder holder, ShoppieObject item1,
			ShoppieObject item2) {
		if (item1 instanceof Campaign) {
			final Campaign cp1 = (Campaign) item1;
			final Campaign cp2 = (Campaign) item2;

			if (item1 != null) {
				new AsyncTask<String, Integer, Bitmap>() {

					@Override
					protected Bitmap doInBackground(String... arg0) {
						Bitmap bmp = cp1.getBitmap(getContext(), cp1.linkImage);
//						if (bmp == null) {
//							String link = cp1.linkImage;
//							link = link.replaceAll(" ", "%20");
//							cp1.getBitmap(getContext(), link);
//						}

						return bmp;
					}

					@Override
					protected void onPostExecute(Bitmap result) {
						if (result != null) {
							holder.avatar1.setImageBitmap(result);
						}
					}
				}.execute("");

				holder.score1.setText(cp1.merchId + "");
				holder.title1.setText(cp1.name);
			}
			if (item2 != null) {
				new AsyncTask<String, Integer, Bitmap>() {

					@Override
					protected Bitmap doInBackground(String... arg0) {
						Bitmap bmp = cp2.getBitmap(getContext(), cp2.linkImage);
//						if (bmp == null) {
//							String link = cp2.linkImage;
//							link = link.replaceAll(" ", "%20");
//							cp2.getBitmap(getContext(), link);
//						}

						return bmp;
					}

					@Override
					protected void onPostExecute(Bitmap result) {
						if (result != null) {
							holder.avatar2.setImageBitmap(result);
						}
					}
				}.execute("");

				holder.score1.setText(cp2.merchId + "");
				holder.title1.setText(cp2.name);
			} else {
				holder.shop2.setVisibility(View.INVISIBLE);
			}
		} else if (item1 instanceof Product) {
			final Product cp1 = (Product) item1;
			final Product cp2 = (Product) item2;

			if (item1 != null) {
				new AsyncTask<String, Integer, Bitmap>() {

					@Override
					protected Bitmap doInBackground(String... arg0) {
						Bitmap bmp = cp1.getBitmap(getContext(),
								cp1.productImage);
//						if (bmp == null) {
//							String link = cp1.productImage;
//							link = link.replaceAll(" ", "%20");
//							cp1.getBitmap(getContext(), link);
//						}
						return bmp;
					}

					@Override
					protected void onPostExecute(Bitmap result) {
						if (result != null)
							holder.avatar1.setImageBitmap(result);
					}

				}.execute("");
				new AsyncTask<String, Integer, Bitmap>() {

					@Override
					protected Bitmap doInBackground(String... arg0) {
						Bitmap bmp = cp1.getBitmap(getContext(),
								cp1.productImage);
//						if (bmp == null) {
//							String link = cp1.productImage;
//							link = link.replaceAll(" ", "%20");
//							cp1.getBitmap(getContext(), link);
//						}
						return bmp;
					}

					@Override
					protected void onPostExecute(Bitmap result) {
						if (result != null) {
							holder.avatar1.setImageBitmap(result);
						}
					}
				}.execute("");
				holder.score1.setText(cp1.likedNumber + "");
				holder.title1.setText(cp1.productName);
			}
			if (item2 != null) {
				new AsyncTask<String, Integer, Bitmap>() {

					@Override
					protected Bitmap doInBackground(String... arg0) {
						Bitmap bmp = cp2.getBitmap(getContext(),
								cp2.productImage);
//						if (bmp == null) {
//							String link = cp2.productImage;
//							link = link.replaceAll(" ", "%20");
//							cp2.getBitmap(getContext(), link);
//						}
						return bmp;
					}

					@Override
					protected void onPostExecute(Bitmap result) {
						if (result != null)
							holder.avatar2.setImageBitmap(result);
					}
				}.execute("");

				holder.score2.setText(cp2.likedNumber + "");
				holder.title2.setText(cp2.productName);
			} else {
				holder.shop2.setVisibility(View.INVISIBLE);
			}
		} else if (item1 instanceof Gift) {
			final Gift cp1 = (Gift) item1;
			final Gift cp2 = (Gift) item2;

			if (item1 != null) {
				new AsyncTask<String, Integer, Bitmap>() {

					@Override
					protected Bitmap doInBackground(String... arg0) {
						Bitmap bmp = cp1.getBitmap(getContext(), cp1.giftImage);
//						if (bmp == null) {
//
//							String link = cp1.giftImage;
//							link = link.replaceAll(" ", "%20");
//							cp1.getBitmap(getContext(), cp1.giftImage);
//						}
						return bmp;
					}

					@Override
					protected void onPostExecute(Bitmap result) {
						if (result != null) {
							holder.avatar1.setImageBitmap(result);
						}
					}
				}.execute("");

				holder.score1.setText(cp1.description + "");
				holder.title1.setText(cp1.pieQty);
			}
			if (item2 != null) {
				new AsyncTask<String, Integer, Bitmap>() {

					@Override
					protected Bitmap doInBackground(String... arg0) {
						Bitmap bmp = cp2.getBitmap(getContext(), cp2.giftImage);
//						if (bmp == null) {
//							String link = cp2.giftImage;
//							link = link.replaceAll(" ", "%20");
//							cp2.getBitmap(getContext(), link);
//						}

						return bmp;
					}

					protected void onPostExecute(Bitmap result) {
						if (result != null) {
							holder.avatar2.setImageBitmap(result);
						}
					};
				}.execute("");
				holder.score2.setText(cp2.description + "");
				holder.title2.setText(cp2.pieQty);
			} else {
				holder.shop2.setVisibility(View.INVISIBLE);
			}
		} else {
			System.out.println(item1.getClass().getName());
		}

	}

	class ShopHolder {
		RelativeLayout shop1, shop2;
		ImageView avatar1, avatar2;
		TextView title1, title2;
		TextView score1, score2;
		ImageButton pie1, pie2;

	}
}